package com.lanhai.crowd.service.impl;

import com.lanhai.crowd.entity.Auth;
import com.lanhai.crowd.entity.AuthExample;
import com.lanhai.crowd.mapper.AuthMapper;
import com.lanhai.crowd.service.api.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public List<Auth> getAll() {

        List<Auth> auths = authMapper.selectByExample(new AuthExample());
        return auths;
    }

    @Override
    public List<Integer> getAssignedAuth(Integer roleId) {

        List<Integer> list = authMapper.selectAssignedAuthByRoleId(roleId);
        return list;
    }

    @Override
    public void saveAuthRelationship(Map<String, List<Integer>> map) {

        // 获取roleId
        Integer roleId = map.get("roleId").get(0);
        // 获取authIdArray
        List<Integer> authIdArray = map.get("authIdArray");

        // 先根据roleId删除存储在中间表的相关权限
        authMapper.deleteAssignedAuthRelationshipByRoleId(roleId);

        // 再根据roleId和authIdArray插入权限信息
        if(null != authIdArray && authIdArray.size() > 0)
            authMapper.insertAuthRelationshipByAuthIds(roleId, authIdArray);

    }

}
